<?php

namespace App\Http\Controllers\Notice;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
use App\Models\Notice\Notice;

/**
 * 消息管理
 */
class NoticeController extends BaseController
{
    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $memberinfo = $this->mangerinfo;
        $isgetall = $request->isgetall ?? 0;
        $list = Notice::getList($request, $memberinfo, $isgetall);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    public function getunreadcount(Request $request)
    {
        $memberinfo = $this->mangerinfo;
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $utype = $memberinfo['utype'] ?? 0;
        $count = Notice::where(['cpid' => $cpid, 'status' => config('constants.notice.unread'),'manger_id'=>$manger_id,'utype'=>$utype])->count();
        if ($count > 0) {
            $list = Notice::where(['cpid' => $cpid, 'status' => config('constants.notice.unread'),'manger_id'=>$manger_id,'utype'=>$utype])->limit(3)->get()->toArray();
        } else {
            $list = [];
        }

        return response()->json([
            'code' => '000',
            'msg' => '操作成功',
            'data' => [
                'list' => $list,
                'count' => $count
            ]
        ]);
    }

    /**
     * 读
     */
    public function dopostread(Request $request)
    {
        $id = $request->id ?? 0;
        $info = Notice::where([['id', $id]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '公告不存在']);
        }

        Notice::where([['id', $id]])->update(['status' => config('constants.notice.read'), 'update_at' => time()]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
